package com.marsscp.cp.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import net.bblot.lottery.PackegeConfig;
import net.bblot.lottery.util.Logger;


/**
 * Created by lottery on 2017/2/28.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    public final static String WXPay = PackegeConfig.PACKAGE_NAME + "wxPay";
    public final static String KEY_errcode = "key_errcode";
    private IWXAPI api;
    private final static String TAG = "WXPayEntryActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, PackegeConfig.WEIXIN_APPID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
//        Toast.makeText(getApplicationContext(),"onReq", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResp(BaseResp resp) {
        Toast.makeText(getApplicationContext(),"onResp", Toast.LENGTH_SHORT).show();

        int code = resp.errCode;
        Intent intent = new Intent(WXPay);
        intent.putExtra(KEY_errcode, resp.errCode);
        sendBroadcast(intent);
        if (code == 0) {

            //显示充值成功的页面和需要的操作
            Logger.e(TAG, "充值成功");
        }

        if (code == -1) {
            //错误
            Logger.e(TAG, "充值错误");
        }

        if (code == -2) {
            //用户取消
            Logger.e(TAG, "充值取消");
        }
        finish();

    }

}
